let url = window.location.href;
let str = decodeURIComponent(url)
let tagId = str.split("?")[1];
let Name = str.split("?")[2];

let tagQuestionApp = new Vue({
    el:"#tagQuestionApp",
    data:{
        tagQuestions:[],
        pageInfo:{},
        pageSize:5,
        tagName:Name,
        isShow:false
    },
    methods:{
        loadTagQuestions(pageNum){
            axios.get("/portal/questionTag/tagQuestion/"+tagId+'/'+pageNum+'/'+this.pageSize+'/'+Name)
                .then(result=>{
                    let r = result.data;
                    console.log(r)
                    if(r.code == OK){
                        this.pageInfo = r.data
                        this.tagQuestions = r.data.list
                        if(this.tagQuestions.length==0){
                            this.isShow = true
                            return
                        }
                        this.isShow = false
                        this.updateDuration ()
                        this.updateTagImage()
                    }

                })
        },
        updateTagImage(){
            let tagQuestions = this.tagQuestions;
            for(let i=0; i<tagQuestions.length; i++){
                let tags = tagQuestions[i].tags;
                if(tags){
                    if(tagId == 0) {
                        let tagImage = '../img/tags/' + tags[0].id + '.jpg';
                        tagQuestions[i].tagImage = tagImage;
                    }else if(tagId==-1){
                        let tagImage = '../img/tags/' + tags[0].id + '.jpg';
                        tagQuestions[i].tagImage = tagImage;
                    }
                    else {
                        let tagImage = '../img/tags/' + tagId + '.jpg';
                        tagQuestions[i].tagImage = tagImage;
                    }
                }
            }
        },
        updateDuration () {
            let tagQuestions = this.tagQuestions;
            for(let i=0; i<tagQuestions.length; i++){
                //创建问题时候的时间毫秒数
                let createtime = new Date(tagQuestions[i].createtime).getTime();
                //当前时间毫秒数
                let now = new Date().getTime();
                let duration = now - createtime;
                if (duration < 1000*60){ //一分钟以内
                    tagQuestions[i].duration = "刚刚";
                }else if(duration < 1000*60*60){ //一小时以内
                    tagQuestions[i].duration =
                        (duration/1000/60).toFixed(0)+"分钟以前";
                }else if (duration < 1000*60*60*24){
                    tagQuestions[i].duration =
                        (duration/1000/60/60).toFixed(0)+"小时以前";
                }else if(duration < 1000*60*60*24*30){
                    tagQuestions[i].duration =
                        (duration/1000/60/60/24).toFixed(0)+"天以前";
                }else {
                    tagQuestions[i].duration =
                        (duration/1000/60/60/24/30/12).toFixed(0)+"年以前";
                }
            }
        }
    },
    created(){
        this.loadTagQuestions(1)
    }
})